/*
 * TextHelper.java
 * business
 *
 * Created by chentao on 2017/4/26.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.utils;

import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.UnderlineSpan;

import com.yidiandao.business.R;

import static com.yidiandao.business.utils.UIUtils.getResources;

/**
 * 文本渲染工具
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/26 0026 14:27
 */
public class TextRenderUtils {

    /**
     * 获取消息中心-资产消息带有下划线的,高亮显示的文本
     *
     * @param normalText    非下划线文本
     * @param highlightText 下划线文本
     * @param highlight     是否高亮
     */
    public static SpannableStringBuilder getAssetHighlightContent(String normalText, String highlightText, boolean highlight) {
        return getHighlightUnderlineText(normalText, R.style.AssetContentPre, highlightText, highlight);
    }

    /**
     * 获取消息中心,发现页-活动消息 高亮文本
     */
    public static SpannableStringBuilder getActivityHighlightContent(String normalText, String highlightText, boolean highlight) {
        return getHighlightUnderlineText(normalText, R.style.ActivityContentPre, highlightText, highlight);
    }

    /**
     * 获取具备下划线与高亮显示的文本
     */
    private static SpannableStringBuilder getHighlightUnderlineText(String normalText, @StyleRes int normalTextStyle,
                                                                    String highlightText, boolean highlight) {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        // 内容部分
        SpannableString preSpannable = getAppearanceText(normalText, normalTextStyle);

        SpannableString clickSpannable;
        if (highlight) {
            clickSpannable = getUnderlineText(highlightText, UIUtils.getColor(R.color.btn_red));
        } else {
            clickSpannable = getUnderlineText(highlightText, UIUtils.getColor(R.color.invoice_info));
        }

        builder.append(preSpannable);
        builder.append(clickSpannable);

        return builder;
    }

    /**
     * 根据 style 渲染文字
     */
    public static SpannableString getAppearanceText(String text, @StyleRes int appearance) {
        SpannableString spannable = new SpannableString(text);
        spannable.setSpan(new TextAppearanceSpan(UIUtils.getContext(), appearance), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannable;
    }

    /**
     * 获取指定颜色的Spannable
     */
    public static SpannableString getForegroundColorText(String text, @ColorInt int color) {
        SpannableString spannable = new SpannableString(text);
        spannable.setSpan(new ForegroundColorSpan(color), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannable;
    }

    /**
     * 给文字添加下划线
     */
    public static SpannableString getUnderlineText(String content, @ColorInt int color) {
        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new UnderlineSpan(), 0, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(color), 0, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 获取指定文字大小的 SpannableString
     *
     * @param pxSize 绝对大小: 像素值
     */
    public static SpannableString getAbsoluteSizeText(String text, int pxSize) {
        SpannableString spannable = new SpannableString(text);
        spannable.setSpan(new AbsoluteSizeSpan(pxSize), 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannable;
    }


    /**
     * 给文字添加下划线
     */
    public static SpannableString getUnderlineText(@StringRes int contentResId, @ColorInt int color) {
        String content = UIUtils.getString(contentResId);
        return getUnderlineText(content, color);
    }

    /**
     * 获取带图片的文字.<b>图片会添加到末尾</b>
     */
    public static Spanned getTextWithImage(@StringRes int strId, int imgResId) {
        String textWithImage = UIUtils.getString(strId) + "<img src='" + imgResId + "'/>";
        return Html.fromHtml(textWithImage, TextRenderUtils.getImageGetterInstance(), null);
    }

    private static Html.ImageGetter getImageGetterInstance() {
        return new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                int id = Integer.parseInt(source);
                Drawable d = getResources().getDrawable(id);
                int height = UIUtils.dp2px(18);
                int width = (int) ((float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight()) * UIUtils.dp2px(18);
                d.setBounds(0, UIUtils.dp2px(-2), width, height);
                return d;
            }
        };
    }

    /**
     * 获取不同字号的商品价格
     *
     * @param inputPrice 商品价格.可能还是 文字或 float
     * @param unit       价格单位.
     * @param largeSize  大字号 px.
     * @param smallSize  小字号 px.
     */
    public static SpannableString getBeautyPrice(String inputPrice, String unit, int largeSize, int smallSize) {
        return getBeautyPriceWithColor(inputPrice, unit, largeSize, smallSize, -1);
    }

    /**
     * 获取不同字号的商品价格
     *
     * @param inputPrice 商品价格.可能还是 文字或 float
     * @param unit       价格单位.
     * @param largeSize  大字号 px.
     * @param smallSize  小字号 px.
     * @param color      格式化后价格字符串的颜色
     */
    public static SpannableString getBeautyPriceWithColor(String inputPrice, String unit, int largeSize, int smallSize, @ColorInt int color) {

        // 未登录的情况,是文字,不是数字
        if (!RegexUtils.isFloat(inputPrice)) {
            return getAbsoluteSizeText(inputPrice, largeSize);
        }

        // 保留两位小数
        inputPrice = NumberFormatUtils.get2DecimalStringValue(Float.parseFloat(inputPrice));

        // 是数字
        int index = inputPrice.indexOf(".");

        if (index == -1) {
            index = inputPrice.length();
        }

        // 整数与小数 价格
        String integerPrice = inputPrice.substring(0, index);
        String pointPrice = inputPrice.substring(index, inputPrice.length());

        int exclusive = Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
        int preLength = integerPrice.length();
        int pointLength = pointPrice.length();

        String finalStr;
        if (TextUtils.isEmpty(unit)) {
            finalStr = "￥" + integerPrice + pointPrice;
        } else {
            finalStr = "￥" + integerPrice + pointPrice + "/" + unit;
        }

        // ￥100.12/瓶
        SpannableString builder = new SpannableString(finalStr);
        builder.setSpan(new AbsoluteSizeSpan(smallSize), 0, 1, exclusive); // ￥
        builder.setSpan(new AbsoluteSizeSpan(largeSize), 1, preLength + 1, exclusive); //100
        builder.setSpan(new AbsoluteSizeSpan(smallSize), preLength + 1, (preLength + 1 + pointLength), exclusive); // .12
        if (unit != null) {
            builder.setSpan(new AbsoluteSizeSpan(smallSize), preLength + 1 + pointLength, finalStr.length(), exclusive); // /瓶
        }

        if (color != -1) {
            // 颜色
            builder.setSpan(new ForegroundColorSpan(UIUtils.getColor(R.color.pink)), 0, builder.length(), exclusive);
        }

        return builder;
    }
}
